import { setToken, removeToken } from "@/utils/token";
import { Module } from "vuex";
import { RootState } from "../types";

export interface UserState {
    name: string;
    token: string;
    isAdmin: boolean;
    username: string;
    id: string; // 用户id
    subsonicSalt: string; // 获取封面歌词等都可能需要这个
    subsonicToken: string; // 这是另一个token, 目前不知道干嘛用的
    lastFMApiKey?: string; // 好像是用来共享的
}

const state: UserState = {
    id: "",
    name: "",
    token: "",
    isAdmin: false,
    username: "",
    subsonicSalt: "",
    subsonicToken: "",
};

const userModule: Module<UserState, RootState> = {
    namespaced: true, // 开启命名空间
    state,
    mutations: {
        SET_ID: (state: any, id: string) => {
            state.id = id;
        },
        SET_SUBSONIC_SALT: (state: any, subsonicSalt: string) => {
            state.subsonicSalt = subsonicSalt;
        },
        SET_SUBSONIC_TOKEN: (state: any, subsonicToken: string) => {
            state.subsonicToken = subsonicToken;
        },
        SET_IS_ADMIN: (state: any, isAdmin: boolean) => {
            state.isAdmin = isAdmin;
        },
        SET_NAME: (state: any, name: string) => {
            state.name = name;
        },
        SET_TOKEN: (state: any, token: string) => {
            state.token = token;
            setToken(token);
        },
        SET_USERNAME: (state: any, username: string) => {
            state.username = username;
        },
        REMOVE_ID: (state: any) => {
            state.id = "";
        },
        REMOVE_SUBSONIC_SALT: (state: any) => {
            state.subsonicSalt = "";
        },
        REMOVE_SUBSONIC_TOKEN: (state: any) => {
            state.subsonicToken = "";
        },
        REMOVE_IS_ADMIN: (state: any) => {
            state.isAdmin = false;
        },
        REMOVE_NAME: (state: any) => {
            state.name = "";
        },
        REMOVE_TOKEN: (state: any) => {
            state.token = "";
            removeToken();
        },
        REMOVE_USERNAME: (state: any) => {
            state.username = "";
        },
    },
    actions: {
        updateUserInfo({ commit }, payload: UserState) {
            return new Promise((resolve) => {
                const {
                    id,
                    name,
                    token,
                    isAdmin,
                    username,
                    subsonicSalt,
                    subsonicToken,
                } = payload;
                commit("SET_ID", id);
                commit("SET_NAME", name);
                commit("SET_TOKEN", token);
                commit("SET_IS_ADMIN", isAdmin);
                commit("SET_USERNAME", username);
                commit("SET_SUBSONIC_SALT", subsonicSalt);
                commit("SET_SUBSONIC_TOKEN", subsonicToken);

                resolve(null);
            });
        },
        logout({ commit }) {
            return new Promise((resolve) => {
                commit("REMOVE_ID");
                commit("REMOVE_NAME");
                commit("REMOVE_TOKEN");
                commit("REMOVE_IS_ADMIN");
                commit("REMOVE_USERNAME");
                commit("REMOVE_SUBSONIC_SALT");
                commit("REMOVE_SUBSONIC_TOKEN");
                resolve(null);
            });
        },
    },
    getters: {},
};

export default userModule;
